var api = require('../../../config/api.js');

//获取应用实例
var app = getApp()
Page({
  data: {
    userName: '',
    userPassword: ''
  },


  onLoad: function() {
    var that = this
    //此处需要请求数据校验token是否失效，如果失效则需要重新登录
    if (wx.getStorageSync('token')) {
      console.info("token=" + wx.getStorageSync('token'));
      that.tokenValidate();
    } else {
      that.checkOpenid()
    }
  },

  onShow: function () {
    var that = this
    if (wx.getStorageSync('openId')) {
      that.setData({
        openid: wx.getStorageSync('openId')
      })
    }
  },


  // 获取输入账号
  phoneInput: function(e) {
    this.setData({
      userName: e.detail.value
    })
  },

  // 获取输入密码
  passwordInput: function(e) {
    this.setData({
      userPassword: e.detail.value
    })
  },

  // 登录
  login: function() {
    if (this.data.userName.length == 0 || this.data.userPassword.length == 0) {
      wx.showToast({
        title: '用户名和密码不能为空',
        icon: 'loading',
        duration: 2000
      })
    } else {
      // 这里修改成跳转的页面
      //开始登录请求
      wx.showLoading({
        title: '登录中',
      })
      this.loginAjax();
    }
  },

  //验证openid
  checkOpenid: function () {
    var that = this
    if (wx.getStorageSync('openId')) {
      that.setData({
        openid: wx.getStorageSync('openId')
      })
    } else {
      wx.navigateTo({
        url: '/pages/login/login',
      })
    }
  },


  loginAjax: function() {
    var that = this
    wx.request({
      url: api.AuthLogin + '?username=' + that.data.userName + '&password=' + that.data.userPassword + '&wxType=AGENT' + '&openId=' + this.data.openid,
      data: {},
      header: {
        "Content-Type": "application/x-www-form-urlencoded"
      },
      method: 'POST',
      success: function(res) {
        if (res.data.success) {
          wx.hideLoading();
          wx.showToast({
            title: '登录成功！',
            icon: 'none',
            duration: 2000
          })

          var id_token = res.data.obj.token;
          if (id_token) {
            console.log(id_token)
            wx.setStorageSync('token', id_token)
            wx.setStorageSync('userName', res.data.obj.user.username)
            wx.setStorageSync('usertype', res.data.obj.user.roleIds)
            console.log(wx.getStorageSync('usertype'))
          }
          wx.request({
            url: api.GetAuthInfo,
            data: {},
            method: 'GET',
            header: {
              'Accept': 'application/json',
              'ACCESS-TOKEN': wx.getStorageSync('token')
            },
            success: (res) => {
              if (res.data.success) {
                wx.setStorageSync('userId', res.data.obj.id)
                wx.setStorageSync('deptId', res.data.obj.deptId)
                console.log(wx.getStorageInfoSync('u wx.hideLoading();erId'))
              }
              wx.switchTab({
                url: '/pages/manage/manage'
              })
            }

          })

        } else {
          wx.showToast({
            title: '账号或密码错误，请重新输入！',
            icon: 'none',
            duration: 3000
          })
          wx.hideLoading();
        }

      },
      fail: function(res) {
        wx.showToast({
          title: '网络故障,登录失败',
          icon: 'none',
          duration: 2000
        })
        wx.hideLoading();
      }
    })
  },
  //校验token
  tokenValidate: function() {
    var token = wx.getStorageSync('token')
    wx.request({
      url: api.CheckToken,
      data: {},
      method: 'GET',
      header: {
        'Accept': 'application/json',
        'ACCESS-TOKEN': wx.getStorageSync('token')
      },
      success: (res) => {
        if (res.data.success) {
          console.log('当前token有效==' + token)
          wx.switchTab({
            url: '/pages/manage/manage'
          })
        } else {
          wx.showToast({
            title: '登录已失效，请重新登录！',
            icon: 'none',
            duration: 2000
          })
          wx.hideLoading();
        }

      }
    })

  },

})